package com.cloud.service.util;


import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 利用反射机制进行对象和Map相互转换
 *
 * @author qinxh
 * @date 2019/09/10
 */
public class BeanAndMapTransform {

    /**
     * 转换bean为map
     *
     * @param source                        要转换的bean
     * @param <T>                           bean类型
     * @return                              转换结果
     */
    public static <T> Map<String, Object> bean2Map(T source){

        try {
            Class<?> sourceClass = source.getClass();
            // 拿到所有的字段,不包括继承的字段
            Field[] sourceFiled = sourceClass.getDeclaredFields();
            Map<String, Object> result = new HashMap<String, Object>(sourceFiled.length);
            for (Field field : sourceFiled) {
                // 设置可访问,不然拿不到private
                field.setAccessible(true);
                result.put(field.getName(), field.get(source));
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    /**
     * map转bean
     * @param source                        map属性
     * @param instance                      要转换的bean对应的类
     * @return                              bean
     */
    public static <T> T map2Bean(Map<String, Object> source, Class<T> instance) {

        try {
            T object = instance.newInstance();
            Field[] fields = object.getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                // 获取字段类型(不包含Date类型)
                Class<?> fieldType = field.getType();
                // 获取字段值
                Object value = source.get(field.getName());
                if (String.class == fieldType) {
                    field.set(object, value);
                } else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {
                    field.set(object, Integer.parseInt(value.toString()));
                } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
                    field.set(object, Long.valueOf(value.toString()));
                } else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {
                    field.set(object, Float.valueOf(value.toString()));
                } else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) {
                    field.set(object, Short.valueOf(value.toString()));
                } else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {
                    field.set(object, Double.valueOf(value.toString()));
                } else if (Character.TYPE == fieldType) {
                    if ((value != null) && (value.toString().length() > 0)) {
                        field.set(object,value.toString().charAt(0));
                    }
                }else{
                    throw new Exception("存在不支持的类型");
                }
            }
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
